/*--------------------------------------------------------------------------------
	Hujub Exporter	
				by Hujub SungiL
	
	HISTORY	: 2005_10_14

    LAST UPDATE : 2005_10_14
--------------------------------------------------------------------------------*/
#pragma once

#include	"SkinStructure.h"
#include	".\\Public\\Extern.h"
#include	"MaterialMng.h"

class CSkinExp
{
public:
	int				m_nNumOfTotNode;
	int				m_nCurNode;
	int				m_nNumOfGeom;
	int				m_nNumOfChildNode;
	Interval		m_Interval;

private:
	Interface*		m_pInterface;
	INode*			m_pLootNode;
	FILE*			m_pFile;
	CMaterialMng	m_MtrlMng;
    Geom*			m_pSkin;
	MtrlInfo		m_MtrlInfo;

public:
	BOOL	InitSkinExp( Interface* pInterface );
	VOID	FillMtrlMng( INode* pNode , int& nNumOfNode );	

    BOOL	Tick();
	BOOL	MtrlExpTick();

	VOID	SetMaterial();
	BOOL	SetSkin();
	VOID	SetOBBbox();

	VOID	WriteMaterial( Mtl* pMtl , int nMtlID , int nSubNo , int* pCount );
	VOID	WriteDetailMaterial( Mtrl* pMtrl , Mtl* pMtl );
	Mtrl*	WriteTexture( Mtrl* pMtrl , Texmap* pTex , Class_ID cid , int nSubNo , float fAmt , int* pCount );
	BOOL	WriteNode( Geom* pSkin , INode* pNode );
	Geom*	WriteGeom( Geom* pSkin , INode* pNode );    
	VOID	WriteBipedInfo( Geom* pSkin , INode* pNode , Modifier* pPhyMod );
	VOID	WriteMesh( Geom* pSkin , INode* pNode , TimeValue tv );
	Geom*	WriteHelper( Geom* pSkin , INode* pNode );
	BOOL	WriteForFileGeom( TCHAR* szSavePath );
	BOOL	WriteForFileMtrl( TCHAR* szFilePath );

    BOOL	GetTexFName( Texmap* pTexmap , TCHAR* szRet );
    TriObject*	GetTriObjectFromNode( INode* pNode , TimeValue tv , int& deleteIt );
	Point3	GetVertexNormal( Mesh* pMesh , int nFaceNo , RVertex* pRVertex );

	VOID	AddMtrl( Mtrl* pMtrl );

	Modifier*	FindPhysiqueModifier( INode* pNode );

	VOID	ExportPhysiqueData( INode* pNode , Modifier* pMod , Geom* pSkin );

	VOID	DefaultOptimize( Geom* pSkin , VertexInfo* pVertex , FaceIndex* pFaceIndex );
	VOID	VertexOrderSort( FaceIndex*	pFaceIndex , VertexInfo* pVertex , const int nNumFace , const int nNumVertex );

	BOOL	FloatCompare( const float fA , const float fB );
    
	FLOAT	Det( D3DXMATRIX &con	, float val );

	VOID	Release();
	VOID	DeleteMng();
	VOID	DeleteMtl( Mtrl* pMtrl );
	VOID	DeleteSkin( Geom* pSkin );
	VOID	DeleteMesh( Geom* pSkin );

	inline	BOOL	TMNegParity( Matrix3 &m ) {
		return (DotProd(CrossProd(m.GetRow(0),m.GetRow(1)),m.GetRow(2))<0.0)?1:0;
	}

public:
	CSkinExp(void) : m_nNumOfTotNode(0) , m_nCurNode(0) , m_nNumOfGeom(0) , m_nNumOfChildNode(0) , m_pInterface(NULL) , 
		m_pLootNode(NULL) , m_pFile(NULL) , m_pSkin(NULL){
			memset( &m_MtrlInfo , 0 , sizeof( MtrlInfo ) );
		}
		~CSkinExp(void){}
};
